package com.text.controller;

import com.text.entity.Course;
import com.text.entity.StudentAndCourse;
import com.text.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    // 获取课程信息（根据 ID）
    @GetMapping("/{id}")
    public ResponseEntity<Course> getCourse(@PathVariable String id) {
        Course course = courseService.getCourse(id);
        if (course == null) {
            return ResponseEntity.notFound().build(); // 如果课程不存在，返回 404
        }
        return ResponseEntity.ok(course); // 返回课程信息
    }

    // 获取所有课程
    @GetMapping
    public List<Course> getAllCourses() {
        return courseService.getAllCourses();
    }

    // 添加课程
    @PostMapping("/add")
    public ResponseEntity<String> addCourse(@RequestBody Course course) {
        courseService.addCourse(course);
        return ResponseEntity.ok("Course added successfully!");
    }

    // 更新课程信息（根据 ID）
    @PutMapping("/{id}")
    public ResponseEntity<String> updateCourse(@PathVariable String id, @RequestBody Course updatedCourse) {
        boolean updated = courseService.updateCourse(id, updatedCourse);
        if (updated) {
            return ResponseEntity.ok("Course updated successfully!");
        } else {
            return ResponseEntity.notFound().build(); // 如果课程不存在，返回 404
        }
    }

    // 删除课程（根据 ID）
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteCourse(@PathVariable String id) {
        boolean deleted = courseService.deleteCourse(id);
        if (deleted) {
            return ResponseEntity.ok("Course deleted successfully!");
        } else {
            return ResponseEntity.notFound().build(); // 如果课程不存在，返回 404
        }
    }
}